package com.zhdl.modules.demo.DCHTProcess;

//import com.zhdl.modules.common.entity.ConfigurationPLc;
//import com.zhdl.modules.common.test.GetPLCValue;

import cn.hutool.core.util.HexUtil;
import com.project.common.util.DelayUtil;
import com.zhdl.common.ytools.BaseConversionTools;
import com.zhdl.common.ytools.BitConversionTools;
import com.zhdl.common.ytools.callback.CommonCallback;
import com.zhdl.hardware_contact.hardware_common.param.CurrencyProtocolReadReq;
import com.zhdl.hardware_contact.hardware_common.param.CurrencyProtocolWriteReq;
import com.zhdl.hardware_contact.hardware_common.param.TcpAssembleCmdPkg;
import com.zhdl.hardware_contact.hardware_config.entity.ProjectConfiguration;
import com.zhdl.modules.process.common.GetPLCValue;
import com.zhdl.network.other_protocol.modbustcp.ModbusTcpRespPkg;
import com.zhdl.network.strategy.common_client.impl.CommonProtocolStrategy;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@Component
@Slf4j
public class CHTSmallRobotInterface implements GetPLCValue {
    @Resource
    ThreadPoolTaskScheduler threadPoolTaskScheduler;
    @Resource
    CommonProtocolStrategy commonProtocolStrategy;
    public final String PICK_DONE = "pick_done";
    public final String PICK_DONE_OK = "pick_done_ok";
    public final String ROBOT_RUNNING = "robot_running";
    public final String MOVE_CAMERA_1_1 = "move_camera_1_1";
    public final String MOVE_CAMERA_1_2 = "move_camera_1_2";
    public final String MOVE_PICK = "move_pick";
    public final static String MOVE_PUT_1 = "move_put_1";
    public final static String MOVE_PUT_2 = "move_put_2";
    public final static String MOVE_PUT_3 = "move_put_3";
    public final static String MOVE_PUT_4 = "move_put_4";
    public final static String MOVE_PUT_5 = "move_put_5";
    public final static String MOVE_PUT_6 = "move_put_6";
    public final static String MOVE_HOME = "move_home";
    public final static String MOVE_WORK = "move_work_1";
    public final static String MOVE_WORK_2 = "move_work_2";
    public final static String DLS_DONE = "dls_done";
    List<ProjectConfiguration> configurationPLcList;
     public final String ROBOT_IP = "192.168.70.68:502";
   // public final String ROBOT_IP = "192.168.60.101:502";
    private String lastFlag ="";

    public void initData() {
        configurationPLcList = new ArrayList<>();
        configurationPLcList.add(new ProjectConfiguration(PICK_DONE, "0", 1));
        configurationPLcList.add(new ProjectConfiguration(PICK_DONE_OK, "0", 1));
        configurationPLcList.add(new ProjectConfiguration(ROBOT_RUNNING, "0", 1));
        configurationPLcList.add(new ProjectConfiguration(MOVE_CAMERA_1_1, "0", 1));
        configurationPLcList.add(new ProjectConfiguration(MOVE_CAMERA_1_2, "0", 1));
        configurationPLcList.add(new ProjectConfiguration(MOVE_PICK, "0", 1));
        configurationPLcList.add(new ProjectConfiguration(MOVE_PUT_1, "0", 1));
        configurationPLcList.add(new ProjectConfiguration(MOVE_PUT_2, "0", 1));
        configurationPLcList.add(new ProjectConfiguration(MOVE_PUT_3, "0", 1));
        configurationPLcList.add(new ProjectConfiguration(MOVE_PUT_4, "0", 1));
        configurationPLcList.add(new ProjectConfiguration(MOVE_PUT_5, "0", 1));
        configurationPLcList.add(new ProjectConfiguration(MOVE_PUT_6, "0", 1));
        configurationPLcList.add(new ProjectConfiguration(MOVE_HOME, "0", 1));
        configurationPLcList.add(new ProjectConfiguration(MOVE_WORK, "0", 1));
        configurationPLcList.add(new ProjectConfiguration(MOVE_WORK_2, "0", 1));

        threadPoolTaskScheduler.scheduleWithFixedDelay(() -> {
            CurrencyProtocolReadReq currencyProtocolReadReq = new CurrencyProtocolReadReq();
            currencyProtocolReadReq.setId("01");
            currencyProtocolReadReq.setReadNumber("0002");
            currencyProtocolReadReq.setStartAddress("000E");
            try {
                commonProtocolStrategy.sendCmd(ROBOT_IP, HexUtil.encodeHexStr(TcpAssembleCmdPkg.read03(currencyProtocolReadReq)), s -> {
                  //  log.info("读到机器人数据" + ModbusTcpRespPkg.getDataFor03(s));
                    s = ModbusTcpRespPkg.getDataFor03(s);
                    String substring = s.substring(0, 4);
                    String s1 = BitConversionTools.HexStrConversionBit(substring);
                    if (s1.contains("1") && !lastFlag.contains("1") ){
                        log.info("机器人完成动作");
                        getValue(PICK_DONE).setValue("1");
                    }
                    lastFlag = s1;
                });
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }, 300);
    }


    public boolean moveByStep() {
        reSetAll();
        CurrencyProtocolWriteReq currencyProtocolReadReq2 = new CurrencyProtocolWriteReq();
        currencyProtocolReadReq2.setId("01");
        currencyProtocolReadReq2.setStartAddress("003F");
        currencyProtocolReadReq2.setWriteValue("FFFF");
        try {
            commonProtocolStrategy.sendCmd(ROBOT_IP, HexUtil.encodeHexStr(TcpAssembleCmdPkg.write10(currencyProtocolReadReq2)), s -> log.info(s));
            DelayUtil.delayedExecution(1000, () -> {
                currencyProtocolReadReq2.setWriteValue("0000");
                try {
                    commonProtocolStrategy.sendCmd(ROBOT_IP, HexUtil.encodeHexStr(TcpAssembleCmdPkg.write10(currencyProtocolReadReq2)), s -> log.info(s));
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            });
        } catch (IOException e) {
            return false;
        }
        return true;
    }


    public void reSetAll(){
        for (ProjectConfiguration projectConfiguration : configurationPLcList) {
            if (projectConfiguration.valueType == 1){
                projectConfiguration.value = "0";
            }
        }
    }
    @Override
    public ProjectConfiguration getValue(String field) {
        for (ProjectConfiguration configurationPLc : configurationPLcList) {
            if (configurationPLc.getField().equals(field)) {
                return configurationPLc;
            }
        }
        return null;
    }


}
